import { message } from 'antd';
import { history } from 'umi';
import { addBindVipCard,updateBindVipCard,fetchAllVipCard,fetchShopList,fetchSalesList,fetchMembersList,fetchVip } from './service';

const Model = {
  namespace: 'cubeVipBindCard',
  state: {
    currentVip:{},
    allVipCard:[],
    shopList:[],
    salesList:[],
    membersList:[]
  },
  effects: {
    *add({ payload }, { call }) {
      let result=yield call(addBindVipCard, payload);
      if(result.code===0){
        message.success('提交成功');
        history.push(`/vip/bind-card`)
      }else{
        message.error(result.message);
      }
    },
    *update({ payload }, { call }) {
      let result=yield call(updateVip, payload);
      if(result.code===0){
        message.success('提交成功');
        history.push(`/vip/center`)
      }else{
        message.error(result.message);
      }
    },
    *fetchAllVipCard({ payload }, { call,put }) {
      let result=yield call(fetchAllVipCard, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            allVipCard:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchShopList({ payload }, { call,put }) {
      let result=yield call(fetchShopList, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            shopList:result.data          }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchSalesList({ payload }, { call,put }) {
      let result=yield call(fetchSalesList, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            salesList:result.data          }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchMembersList({ payload }, { call,put }) {
      let result=yield call(fetchMembersList, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            membersList:result.data          }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchMember({ payload }, { call,put }) {
      let result=yield call(fetchVip, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            membersList:[result.data]
          }
        });
      }else{
        message.error(result.message);
      }
    },
  },
  reducers: {
    common(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
export default Model;
